<template>
  <div class="adjust-table">
    <XcTable ref="tableView" :state="loading ? 'loading' : ''" :indexColumn="false" isFullLayout
      highlightCurrentRow :config="titleList" :values="data" :isEditable="isEdit" :editable="editable"></XcTable>
  </div>
</template>

<script lang="ts" setup>
import { ref, nextTick, computed } from "vue";

import type { TreeItem, ResultItem } from "../../api/adjust/index.d";
import { apiMap, tableMap } from "../index";
// import { useAuthStore } from "@/store/modules/auth";
import type { Editable } from "@xc-components/xc-table/types";
const authStore = {
  calibrationName: "开场校准管理器"
};
const emits = defineEmits(["update", "updateChart"]);
const props = withDefaults(defineProps<{ data: ResultItem[]; current: Partial<TreeItem>; isEdit: boolean; loading: boolean }>(), {
  data: () => [],
  current: () => ({}),
  isEdit: false,
  loading: false
});
const titleList = computed(() => tableMap[props.current.pId!!]);
const tableView = ref();
/**
 * @description: 表格编辑配置
 * @return {*}
 */
const editable = ref<Editable>({
  editText: "",
  onSave: async (row: ResultItem, done?: (isEditable: boolean, data: any) => void) => {
    delete (row as any).isEditable;
    const { addResultKey, addResult, addResultCaseIdKey } = apiMap[props.current.pId!!];
    await addResult({ [addResultKey!!]: { ...row, [addResultCaseIdKey!!]: props.current.id } }, { calibrationName: authStore.calibrationName });
    done && done(false, row);
    emits("update");
    emits("updateChart");
  },
  onDelete: async (row: ResultItem) => {
    const { removeResultKey, removeResult } = apiMap[props.current.pId!!];
    await removeResult({ [removeResultKey!!]: row.id }, { calibrationName: authStore.calibrationName });
    emits("updateChart");
  }
});
/**
 * @description: 滚动条到底部
 * @param {*} isReset
 * @return {*}
 */
const scrollBottom = (isReset = false) => {
  nextTick(() => {
    const table = tableView.value.table.layout.table.refs;
    const ele = table.bodyWrapper.firstElementChild.firstElementChild;
    ele.scrollTop = isReset ? 0 : ele.scrollHeight;
  });
};
defineExpose({ scrollBottom });
</script>
<style scoped lang="scss">
.adjust-table {
  height: 100%;

  :deep(.xc-table) {
    height: 100%;
    --table-main-padding-spacing: 0;
  }

  .table-wrap {
    position: relative;

    &::after {
      content: "";
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      border-bottom: 1px solid #eaeaea;
    }
  }

  :deep(.el-table) {
    font-size: 12px;

    .cell {
      height: 32px;
      line-height: 32px !important;
    }

    .el-table__cell {
      padding: 0;
    }

    .el-table__header {
      th {
        background-color: #f4f4f4 !important;
      }
    }

    .el-input {
      .el-input__inner {
        border-color: transparent;
        height: 26px;
        line-height: 26px;

        &:hover {
          border-color: #eaeaea;
        }
      }
    }

    .current-row {
      .el-input {
        .el-input__inner {
          border-color: #eaeaea;

          &:hover {
            border-color: #bdbdbd;
          }

          &:focus {
            border-color: #008efa;
          }
        }
      }
    }
  }
}
</style>
